package personnel.department.pages.addpage;

import java.util.List;
import java.util.Set;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.jface.viewers.TableViewer;

import personnel.department.dao.entity.Diplom;
import personnel.department.dao.entity.Internship;
import personnel.department.pages.PDTable;
import personnel.department.pages.widgets.AbstractWidget;

public class InternshipEditor extends AbstractTableEditor<Internship> {

	public InternshipEditor(CTabFolder tabFolder) {
		super(tabFolder, "Интернатура/ординатура");
	}
	
	public InternshipEditor(CTabFolder tabFolder, Set<Internship> internships) {
		super(tabFolder, "Интернатура/ординатура", internships);
	}

	@Override
	public PDTable<Internship> createTable(Composite parent) {
		return new PDTable<Internship>(parent) {
			
			@Override
			public List<Internship> setModelProvider() {
				return getModel();
			}

			@Override
			protected void createColumns(Composite parent, TableViewer viewer) {
				String[] titles = { "Серия", "Номер диплома", "Период обучения", "Интернатура", "Ординатура", "Специальность", "Целевая подготовка"};
			    int[] bounds = { 100, 100, 100, 100, 100, 100, 100};

			    TableViewerColumn col = createTableViewerColumn(getViewer(), titles[0], bounds[0], 0);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			       Internship e = (Internship) element;
			        return String.valueOf(e.getDiplom().getSeries());
			      }
			    });

			    col = createTableViewerColumn(getViewer(), titles[1], bounds[1], 1);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			    	  Internship e = (Internship) element;
				        return String.valueOf(e.getDiplom().getDiplomNumber());
			      }
			    });

			    col = createTableViewerColumn(getViewer(), titles[2], bounds[2], 2);
			    col.setLabelProvider(new ColumnLabelProvider() {
			      @Override
			      public String getText(Object element) {
			    	  Internship e = (Internship) element;
			    	  Diplom diplom = e.getDiplom();
				        return format.format(diplom.getStartEducation()) + " - " + format.format(diplom.getFinishEducation());
			      }
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[3], bounds[3], 3);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
				    	  Internship e = (Internship) element;
					        return String.valueOf(e.getInternship());
			    	}
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[4], bounds[4], 4);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
				    	  Internship e = (Internship) element;
					        return String.valueOf(e.getTraineeship());
			    	}
			    });
			    			    
			    col = createTableViewerColumn(getViewer(), titles[5], bounds[5], 5);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
			    		Internship e = (Internship) element;
			    		return e.getSpeciality();
			    	}
			    });
			    
			    col = createTableViewerColumn(getViewer(), titles[6], bounds[6], 6);
			    col.setLabelProvider(new ColumnLabelProvider() {
			    	@Override
			    	public String getText(Object element) {
			    		Internship e = (Internship) element;
			    		return String.valueOf(e.getTargetTraining());
			    	}
			    });

			}
		};
	}
	
	@Override
	protected void validate() {
		AbstractWidget<Boolean> traineeship = (AbstractWidget<Boolean>) getWidgetByFieldName("traineeship");
		AbstractWidget<Boolean> internship = (AbstractWidget<Boolean>) getWidgetByFieldName("internship");
		getModel().stream().forEach(intern -> {
			if (intern.getInternship() && intern.getTraineeship()) {
				traineeship.setEnabled(false);
				internship.setEnabled(false);
			}
			if (intern.getInternship()) {
				internship.setEnabled(false);
			}
			if (intern.getTraineeship()) {
				traineeship.setEnabled(false);
			}
			
		});
	}

}
